package meijuli.com.join51.business.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.ResFlowObj;
import meijuli.com.join51.business.model.Wallet;
import meijuli.com.join51.business.utils.TimeUtil;

/**
 * 流水详情页
 * Created by Administrator on 2017/1/11.
 */
public class FlowDetailFragment extends BaseFragment {

    private static final String KEY_RES_FLOW_OBJ = "resFlowObj";
    private static final String KEY_RES_FLOW_OBJ_TYPE = "resFlowObj_type";
    TextView mTvMoney;
    TextView mTvType;
    TextView mTvTime;
    TextView mTvOrderId;
    TextView mToolbarTitle;
    Toolbar mToolbar;
    TextView mTime;
    TextView transactionText;
    TextView mTextStamp;
    ImageView mImageStamp;
    RelativeLayout mRlOrderNum;
    RelativeLayout mRlOrderStatus;

    private ResFlowObj mResFlowObj;
    private int mType;

    public static FlowDetailFragment newInstance(ResFlowObj resFlowObj, int type) {
        FlowDetailFragment fragment = new FlowDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_RES_FLOW_OBJ, resFlowObj);
        bundle.putSerializable(KEY_RES_FLOW_OBJ_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_flow_detail, container, false);
        mTvMoney = view.findViewById(R.id.tv_money);
        mTvType = view.findViewById(R.id.tv_type);
        mTvTime = view.findViewById(R.id.tv_time);
        mTvOrderId = view.findViewById(R.id.tv_orderId);
        mToolbarTitle = view.findViewById(R.id.toolbar_title);
        mToolbar = view.findViewById(R.id.toolbar);
        mTime = view.findViewById(R.id.time);
        transactionText = view.findViewById(R.id.tv_transactionId);
        mTextStamp = view.findViewById(R.id.text_order_stamp);
        mImageStamp = view.findViewById(R.id.image_order_stamp);
        mRlOrderNum = view.findViewById(R.id.rl_order_num);
        mRlOrderStatus = view.findViewById(R.id.rl_order_status);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initToolBar();
        if (getArguments() != null) {
            mResFlowObj = (ResFlowObj) getArguments().getSerializable(KEY_RES_FLOW_OBJ);
            mType = getArguments().getInt(KEY_RES_FLOW_OBJ_TYPE);
            initView();
        }
    }

    private void initToolBar() {
        mToolbar.setTitle("");
        mToolbarTitle.setText("流水明细详情");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });
    }

    private void initView() {
        if (mResFlowObj != null) {
            mTvMoney.setText(String.format(getString(R.string.RMB), mResFlowObj.getStringAmount()));
            mTvTime.setText(TimeUtil.getFormatTime(mResFlowObj.getCtime(), "yyyy-MM-dd HH:mm:ss"));
            mTvOrderId.setText(mResFlowObj.getOrderId());
            mTvType.setText(mResFlowObj.getTypeMsg());
            transactionText.setText(mResFlowObj.getDmId());

            if (!TextUtils.isEmpty(mResFlowObj.getExtractPic())) {
                mTextStamp.setVisibility(View.VISIBLE);
                Glide.with(getActivity()).load(mResFlowObj.getExtractPic()).error(R.mipmap.img_default).into(mImageStamp);
            } else {
                mRlOrderStatus.setVisibility(View.GONE);
            }
        }
        mRlOrderNum.setVisibility(mType == Wallet.TYPE_WITHDRAW ? View.GONE : View.VISIBLE);
    }


}
